#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

void log_message(const char *message) {
    printf("%s\n", message);
}


void run_command_popen(const char *command, const char *message){
    // 定义要执行的命令
    // const char *command = "rpm -ivh elasticsearch-7.9.0.20240508-el7.x86_64.rpm --nodeps 2>&1"; // 将标准错误输出重定向到标准输出
    log_message(message);
    char buffer[256];
    FILE *fp;

    // 使用 popen 执行命令并获取输出
    fp = popen(command, "r");
    if (fp == NULL) {
        perror("Failed to run command");
        exit(EXIT_FAILURE);
    }

    // 读取输出
    int found_already_installed = 0;
    while (fgets(buffer, sizeof(buffer), fp) != NULL) {
        // 输出捕获到的每一行
        printf("%s", buffer);

        // 检查是否包含“已安装”的信息
        if (strstr(buffer, "已安装") != NULL) {
            found_already_installed = 1;
        }
    }

    // 获取命令的返回值
    int result = pclose(fp);
    if (result == -1) {
        perror("pclose failed");
        exit(EXIT_FAILURE);
    }

    // 处理返回值
    if (found_already_installed) {
        printf("Warning: The package is already installed.\n");
    } else {
        // 通过 WIFEXITED 和 WEXITSTATUS 判断
        if (WIFEXITED(result)) {
            int exit_status = WEXITSTATUS(result);
            if (exit_status == 0) {
                printf("Exec Successfully.\n");
            } else {
                printf("Error: exec command %s failed with exit status %d.\n",command, exit_status);
            }
        } else {
            printf("Error: exec command did not terminate normally.\n");
        }
    }
}

//void run_command_popen(const char *command, const char *message){
//    // 定义要执行的命令
//    // const char *command = "rpm -ivh elasticsearch-7.9.0.20240508-el7.x86_64.rpm --nodeps 2>&1"; // 将标准错误输出重定向到标准输出
//    log_message(message);
//    char buffer[256];
//    FILE *fp;
//
//    // 使用 popen 执行命令并获取输出
//    fp = popen(command, "r");
//    if (fp == NULL) {
//        perror("Failed to run command");
//        exit(EXIT_FAILURE);
//    }
//
//    // 读取输出
//    int found_already_installed = 0;
//    while (fgets(buffer, sizeof(buffer), fp) != NULL) {
//        // 输出捕获到的每一行
//        printf("%s", buffer);
//
//        // 检查是否包含“已安装”的信息
//        if (strstr(buffer, "已安装") != NULL) {
//            found_already_installed = 1;
//        }
//    }
//
//    // 获取命令的返回值
//    int result = pclose(fp);
//    if (result == -1) {
//        perror("pclose failed");
//        exit(EXIT_FAILURE);
//    }
//
//    // 处理返回值
//    if (found_already_installed) {
//        printf("Warning: The package is already installed.\n");
//    } else {
//        // 通过 WIFEXITED 和 WEXITSTATUS 判断
//        if (WIFEXITED(result)) {
//            int exit_status = WEXITSTATUS(result);
//            if (exit_status == 0) {
//                printf("Exec Successfully.\n");
//            } else {
//                printf("Error: Package installation failed with exit status %d.\n", exit_status);
//            }
//        } else {
//            printf("Error: Package installation did not terminate normally.\n");
//        }
//    }
//}

